package com.tianji.jersey.resource;




import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;
import javax.ws.rs.Consumes;
import javax.ws.rs.FormParam;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.tianji.commons.vo.result.IntResult;
import com.tianji.jersey.resource.service.MockService;
@Scope("prototype")
@Component
@Path("/client")
public class TestClientResource {
	private Log log = LogFactory.getLog(this.getClass());
	private static int i = 0;
	private Map<String,String> testPrototype = new HashMap<String,String>();
	@Resource
	private MockService mockService;
	@GET
	@Path("test")
	@Produces({"application/xml","application/json"})
	public JAXBXmlRootElement te() throws IOException {
		return new JAXBXmlRootElement("hahahaa");
	}
	@GET
	@Path("get1")
	@Produces({"application/xml","application/json"})
	public AnswerVoList get1() throws IOException {
		log.debug("=========get1================");
		testPrototype.put(++i+"", "time"+System.currentTimeMillis());
		log.debug("message -->"+mockService.getMessage());
		log.debug("map size============>"+testPrototype.size());
		return this.getList(2);
	}
	@POST
	@Path("post4")
	@Produces({"application/xml","application/json"})
	public void post4(String xml) throws IOException {
		log.debug("=========post1================"+xml);
		
	}
	@POST
	@Path("post1")
	@Produces({"application/xml","application/json"})
	@Consumes("application/xml")
	public IntResult post1(AnswerVoList list) throws IOException {
		log.debug("=========post1================");
		log.debug("list size-->"+list.getAnswerVoList().size());
		log.debug("list elem-title->"+list.getAnswerVoList().get(list.getAnswerVoList().size()-1).getAnswerTitle());
		IntResult result = new IntResult();
		result.setValue(list.getAnswerVoList().size());
		return result;
	}
	@POST
	@Path("postForm")
	@Produces({"application/xml","application/json"})
	public IntResult postForm(@FormParam("name") String name,@FormParam("age") int age) throws IOException {
		log.debug("=========post1================");
		log.debug("name-->"+name);
		log.debug("age->"+age);
		IntResult result = new IntResult();
		result.setValue(age);
//		result.setValue(list.getAnswerVoList().size());
		return result;
	}
	
	private AnswerVoList getList(int size){
		AnswerVoList list = new AnswerVoList();
		for(int i=0;i<size;i++){
			AnswerVO vo = new AnswerVO();
			vo.setAnswerId(i);
			vo.setAnswerTitle("测试"+i);
			list.getAnswerVoList().add(vo);
		}
		return list;
	}
	
}
